const app = getApp();
// pages/index/confirmOrder/confirmOrder.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totalPrice: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var cartList = wx.getStorageSync("cartList");
    this.setData({
      cartList: cartList, 
    })
    this.calTotalPrice()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /**
   * 
   */
  calTotalPrice(){
    var cartList = this.data.cartList
    var totalPrice = 0.0;
    for(var i = 0; i < cartList.length; i++){
      totalPrice += cartList[i].goodsPrice * cartList[i].num
    }
    this.setData({
      totalPrice: totalPrice, 
    })
  },

    // 商品描述输入
    onRemarkInput(e) {
      this.setData({
        remark: e.detail
      });
    },

  takeOrder(){
    var _this = this;
    wx.showModal({
      content: '确定下单？',
      showCancel: true,
      success: (res) => {
        if(res.confirm){
          _this.genOrder()
        }else{
          wx.showToast({
            title: '取消操作',
            icon: 'none' 
          })
        }
      }
    })
  },

  wxpay(orderId, orderNo){
    // wx.showModal({
    //   content: '模拟微信支付',
    //   showCancel: true,
    //   success: (res) => {
    //     if(res.confirm){
    //       wx.reLaunch({
    //         url: '/pages/user/order/orderDetail/orderDetail?orderId=' + orderId + '&orderNo=' + orderNo,
    //       })
    //     }
    //   }
    // })
    wx.reLaunch({
      url: '/pages/user/order/orderDetail/orderDetail?orderId=' + orderId + '&orderNo=' + orderNo,
    })
  },

  /**
   * 生成订单
   */
  genOrder(){
    // 生成参数
    var _this = this;
    var remark = _this.data.remark;
    var openId = wx.getStorageSync('openId');
    var cartList = _this.data.cartList;
    var param = {};
    param.remark = remark;
    param.openId = openId;
    var orderGoodsVOList = [];
    var orderCouponVOList = [];
    for(var i = 0; i < cartList.length; i++){
      orderGoodsVOList.push({goodsId: cartList[i].goodsId, num: cartList[i].num})
    }
    var couponId = _this.data.couponId
    orderCouponVOList.push({id: couponId})
    param.orderGoodsVOList = orderGoodsVOList;
    param.orderCouponVOList = orderCouponVOList;
    app.request('/order/takeOrder', 'POST', param).then(data => {
      console.log(data);
      if(data.code == 0){
        wx.setStorageSync("cartList", []);
        _this.wxpay(data.orderDO.orderId, data.orderDO.orderNo);
      }else{
        wx.showToast({
          title: data.msg,
          icon: 'none' 
        })
      }
    }).catch(err => {
      console.log(err)
      wx.showToast({
        title: '下单失败',
        icon: 'none' 
      })
    });
  }
})